package com.ruoyi.data.tdengine.mapper;

import com.ruoyi.data.tdengine.domain.AvgForTimeInterval;
import com.ruoyi.data.tdengine.domain.Sensor;
import com.ruoyi.data.tdengine.domain.SensorDataOfEquipment;
import com.ruoyi.data.tdengine.domain.ValueAvg;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by james-YU on 2020/12/22
 * DESC:
 */
@Repository
public interface SensorMapper {

    int save(Sensor sensor);

    int saveBatch(List<Sensor> data);

    List<Sensor> listAll();

    int findSensorCode(@Param("sensorCode")String sensorCode);

    List<ValueAvg> getAvgForTimeInterval(AvgForTimeInterval avgForTimeInterval);

    void dropOldSensor(@Param("sensorCode")String sensorCode);

    void createTable(Map<String, Object> map);

    List<Sensor> findSensorDataOfEquipment(SensorDataOfEquipment sensorDataOfEquipment);

    Sensor findLastSensorData(SensorDataOfEquipment sensorDataOfEquipment);


    Double getAvgForTimeLimit(SensorDataOfEquipment sensorDataOfEquipment);

    @Select("select * from sensor;")
    List<Sensor> findAll();

    @Select("select avg(value) as value from #{sensorCode} where ts>=now-30d INTERVAL(1d);")
    List<Sensor> findTimeData(@Param("sensorCode") String sensorCode);

    @Select("select avg(value) as value from #{sensorCode} where ts>=#{planBeginTime} and ts<=#{planEndTime} INTERVAL(1d);")
    List<Sensor> getChart(@Param("planBeginTime") Date planBeginTime,@Param("planEndTime") Date planEndTime,@Param("sensorCode") String sensorCode);

    @Select("select avg(value) as value from #{sensorCode} where  ts<=#{planEndTime} INTERVAL(1d);")
    List<Sensor> getChartAndplanEndTime(@Param("planEndTime") Date planEndTime,@Param("sensorCode") String sensorCode);

    @Select("select avg(value) as value from #{sensorCode} where ts>=#{planBeginTime}  INTERVAL(1d);")
    List<Sensor> getChartAndPlanBeginTime(@Param("planBeginTime")Date planBeginTime,@Param("sensorCode") String sensorCode);


    //按小时统计
    @Select("select avg(value) as value from #{sensorCode} where ts>=#{planBeginTime} and ts<=#{planEndTime}  INTERVAL(1h);")
    List<Sensor> getChartByHour(@Param("planBeginTime") Date planBeginTime,@Param("planEndTime") Date planEndTime,@Param("sensorCode") String sensorCode);

    //按5分钟统计
    @Select("select avg(value) as value from #{sensorCode} where ts>=#{planBeginTime} and ts<=#{planEndTime}  INTERVAL(5m);")
    List<Sensor> getChartMinute(@Param("planBeginTime") Date planBeginTime,@Param("planEndTime") Date planEndTime,@Param("sensorCode") String sensorCode);

    //在配置的时间段内还超过预警值，即处理
    @Select("select  last(ts) as ts, last(sensor_code) as sensor_code, avg(value) as value from #{sensorCode} where value>=#{warnThreshold} and ts >=now-#{timeInterval}m;")
    Sensor findSensorData(@Param("sensorCode") String sensorCode,@Param("warnThreshold")  Double warnThreshold,@Param("timeInterval") int timeInterval);

    @Insert("insert into #{sensorCode} (ts,sensor_code,value) values(now,#{sensorCode},#{value});")
    void insertSensorData(@Param("sensorCode")String sensorCode,@Param("value") Double value);

    @Select("select * from #{sensorCode}")
    List<Sensor> selectSensorData(String sensorCode);

    @Insert("insert into #{sensorCode} (ts,sensor_code,value) values(now-#{time}d,#{sensorCode},#{value});")
    void insertSensorBatchData(@Param("sensorCode")String sensorCode,@Param("value") Double value,@Param("time") int time);

    @Select("SELECT * FROM sensor;")
    List<String> selectSensorTest();
}
